< Summary

Class:GDX.Reflection
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Reflection.cs
Covered lines:118
Uncovered lines:0
Coverable lines:118
Total lines:284
Line coverage:100% (118 of 118)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:11
Method coverage:100% (11 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Reflection()0%110100%
GetDefault(...)0%3.043083.33%
GetType(...)0%33092.86%
InvokeStaticMethod(...)0%4.14081.82%
InvokeMethod(...)0%440100%
SetFieldOrPropertyValue(...)0%4.494068.75%
SetFieldValue(...)0%3.053081.82%
SetPropertyValue(...)0%3.053081.82%
TryGetFieldValue[T](...)0%3.513061.54%
TryGetFieldOrPropertyValue(...)0%5.194057.89%
TryGetPropertyValue[T](...)0%3.513061.54%

File(s)

./Packages/com.dotbunny.gdx/GDX/Reflection.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Reflection;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     A collection of reflection related helper utilities.
 12    /// </summary>
 13    /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks>
 14    public static class Reflection
 15    {
 16        /// <summary>
 17        ///     <see cref="BindingFlags"/> for a private field.
 18        /// </summary>
 19        public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic;
 20        /// <summary>
 21        ///     <see cref="BindingFlags"/> for a private static.
 22        /// </summary>
 23        public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic;
 24        /// <summary>
 25        ///     <see cref="BindingFlags"/> for a public static.
 26        /// </summary>
 27        public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public;
 28
 29        /// <summary>
 30        ///     The assembly qualified name for <see cref="UnityEngine.Object" />
 31        /// </summary>
 232        public static readonly string UnityObjectName = typeof(UnityEngine.Object).AssemblyQualifiedName;
 33
 34        /// <summary>
 35        ///     Returns the default value for a given type.
 36        /// </summary>
 37        /// <param name="type">A qualified type.</param>
 38        /// <returns>The default value.</returns>
 39        public static object GetDefault(this Type type)
 240        {
 241            if (type.IsClass || !type.IsValueType)
 142            {
 143                return null;
 44            }
 145            return Activator.CreateInstance(type);
 246        }
 47
 48        /// <summary>
 49        ///     Returns a qualified type..
 50        /// </summary>
 51        /// <param name="type">The full name of a type.</param>
 52        /// <returns>A <see cref="System.Type"/> if found.</returns>
 53        public static Type GetType(string type)
 2954        {
 2955            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
 2956            int loadedAssembliesCount = loadedAssemblies.Length;
 511657            for (int i = 0; i < loadedAssembliesCount; i++)
 255658            {
 255659                Type targetType = loadedAssemblies[i].GetType(type);
 255660                if (targetType != null)
 2761                {
 2762                    return targetType;
 63                }
 252964            }
 265            return null;
 2966        }
 67
 68        /// <summary>
 69        ///     Invokes a known static method.
 70        /// </summary>
 71        /// <param name="type">The explicit type of the static class.</param>
 72        /// <param name="method">The name of the method to invoke.</param>
 73        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 74        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 75        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 76        public static object InvokeStaticMethod(string type, string method, object[] parameters = null,
 77            BindingFlags flags = PublicStaticFlags)
 678        {
 679            Type targetType = GetType(type);
 680            if (targetType != null)
 581            {
 582                MethodInfo targetMethod = targetType.GetMethod(method, flags);
 583                if (targetMethod != null)
 484                {
 485                    return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray);
 86                }
 187            }
 288            return null;
 689        }
 90
 91        /// <summary>
 92        ///     Invoke a known private method on an object.
 93        /// </summary>
 94        /// <param name="targetObject">The ambiguous object to invoke a method on.</param>
 95        /// <param name="method">The name of the method to invoke.</param>
 96        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 97        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 98        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 99        public static object InvokeMethod(object targetObject, string method, object[] parameters = null,
 100            BindingFlags flags = PrivateFieldFlags)
 25101        {
 25102            Type targetType = targetObject.GetType();
 25103            MethodInfo targetMethod = targetType.GetMethod(method, flags);
 25104            return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null;
 25105        }
 106
 107        /// <summary>
 108        ///     Set the field or property value of a specific <paramref name="targetObject"/>, which may not be
 109        ///     normally accessible.
 110        /// </summary>
 111        /// <param name="targetObject">The instanced object which will have it's field or property value set.</param>
 112        /// <param name="name">The field or property's name to set.</param>
 113        /// <param name="value">The value to set the field or property to.</param>
 114        /// <param name="fieldFlags">The field's access flags.</param>
 115        /// <param name="propertyFlags">The property's access flags.</param>
 116        /// <returns>true/false if the value was set.</returns>
 117        public static bool SetFieldOrPropertyValue(object targetObject, string name, object value,
 118            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 6119        {
 6120            if (targetObject == null)
 1121                return false;
 122
 5123            Type type = targetObject.GetType();
 5124            FieldInfo f = type.GetField(name, fieldFlags);
 5125            if (f != null)
 2126            {
 2127                f.SetValue(targetObject, value);
 2128                return true;
 129            }
 130
 3131            PropertyInfo p = type.GetProperty(name,propertyFlags);
 3132            if (p != null)
 2133            {
 2134                p.SetValue(targetObject, value);
 2135                return true;
 136            }
 137
 1138            return false;
 6139        }
 140
 141        /// <summary>
 142        ///     Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible.
 143        /// </summary>
 144        /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th
 145        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 146        /// <param name="name">The field's name to set.</param>
 147        /// <param name="value">The value to set the field to.</param>
 148        /// <param name="flags">The field's access flags.</param>
 149        /// <returns>true/false if the value was set.</returns>
 150        public static bool SetFieldValue(object targetObject, Type type, string name, object value,
 151            BindingFlags flags = PrivateFieldFlags)
 6152        {
 6153            if (type != null)
 5154            {
 5155                FieldInfo field = type.GetField(name, flags);
 5156                if (field != null)
 4157                {
 4158                    field.SetValue(targetObject, value);
 4159                    return true;
 160                }
 1161            }
 2162            return false;
 6163        }
 164
 165        /// <summary>
 166        ///     Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib
 167        /// </summary>
 168        /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if
 169        /// <param name="type">The type of the <paramref name="targetObject"/>.</param>
 170        /// <param name="name">The property's name to set.</param>
 171        /// <param name="value">The value to set the property to.</param>
 172        /// <param name="flags">The property's access flags.</param>
 173        /// <returns>true/false if the value was set.</returns>
 174        public static bool SetPropertyValue(object targetObject, Type type, string name, object value,
 175            BindingFlags flags = PrivateFieldFlags)
 4176        {
 4177            if (type != null)
 3178            {
 3179                PropertyInfo property = type.GetProperty(name, flags);
 3180                if (property != null)
 2181                {
 2182                    property.SetValue(targetObject, value);
 2183                    return true;
 184                }
 1185            }
 2186            return false;
 4187        }
 188
 189        /// <summary>
 190        ///     Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a
 191        /// </summary>
 192        /// <remarks></remarks>
 193        /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t
 194        /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param>
 195        /// <param name="name">The field's name to read.</param>
 196        /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be
 197        /// <param name="flags">The field's access flags.</param>
 198        /// <typeparam name="T">The type of data being read from the field.</typeparam>
 199        /// <returns>true/false if the process was successful.</returns>
 200        public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl
 7201        {
 7202            if (type == null)
 1203            {
 1204                returnValue = default;
 1205                return false;
 206            }
 6207            FieldInfo fieldInfo = type.GetField(name, flags);
 6208            if (fieldInfo == null)
 1209            {
 1210                returnValue = default;
 1211                return false;
 212            }
 5213            returnValue = (T)fieldInfo.GetValue(targetObject);
 5214            return true;
 7215        }
 216
 217        /// <summary>
 218        ///     Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not
 219        ///     be normally accessible.
 220        /// </summary>
 221        /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks>
 222        /// <param name="targetObject">The instanced object which will have it's field or property value read.</param>
 223        /// <param name="name">The field or property's name to read.</param>
 224        /// <param name="returnValue">The returned value from the field or property, the default value if the property w
 225        /// <param name="fieldFlags">The field's access flags.</param>
 226        /// <param name="propertyFlags">The property's access flags.</param>
 227        /// <returns>true/false if a value was found.</returns>
 228        public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue,
 229            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 7230        {
 7231            if (targetObject == null)
 1232            {
 1233                returnValue = null;
 1234                return false;
 235            }
 236
 6237            Type type = targetObject.GetType();
 6238            FieldInfo f = type.GetField(name, fieldFlags);
 6239            if (f != null)
 2240            {
 2241                returnValue = f.GetValue(targetObject);
 2242                return true;
 243            }
 244
 4245            PropertyInfo p = type.GetProperty(name,propertyFlags);
 4246            if (p != null)
 3247            {
 3248                returnValue = p.GetValue(targetObject);
 3249                return true;
 250            }
 251
 1252            returnValue = default;
 1253            return false;
 7254        }
 255
 256        /// <summary>
 257        ///     Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible.
 258        /// </summary>
 259        /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i
 260        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 261        /// <param name="name">The property's name to read.</param>
 262        /// <param name="returnValue">The returned value from the property, the default value if the property was unable
 263        /// <param name="flags">The property's access flags.</param>
 264        /// <typeparam name="T">The type of data being read from the property.</typeparam>
 265        /// <returns>true/false if the process was successful.</returns>
 266        public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin
 5267        {
 5268            if (type == null)
 1269            {
 1270                returnValue = default;
 1271                return false;
 272            }
 4273            PropertyInfo propertyInfo = type.GetProperty(name, flags);
 4274            if (propertyInfo == null)
 1275            {
 1276                returnValue = default;
 1277                return false;
 278            }
 279
 3280            returnValue = (T)propertyInfo.GetValue(targetObject);
 3281            return true;
 5282        }
 283    }
 284}

Coverage by test methods































































































































































































































































































































































































































































































































































































































































































Methods/Properties

Reflection()
GetDefault(System.Type)
GetType(System.String)
InvokeStaticMethod(System.String, System.String, System.Object[], System.Reflection.BindingFlags)
InvokeMethod(System.Object, System.String, System.Object[], System.Reflection.BindingFlags)
SetFieldOrPropertyValue(System.Object, System.String, System.Object, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
SetFieldValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
SetPropertyValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
TryGetFieldValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)
TryGetFieldOrPropertyValue(System.Object, System.String, System.Object&, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
TryGetPropertyValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)